package GUI;

import java.awt.*;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JOptionPane;

import Messages.MessageDeleteEmployee;
import Messages.MessageGetReportWaitersByDate;
import Messages.MessageGetReportWaitersByDateReply;
import Messages.MessageGetUserById;
import Messages.MessageGetUserByIdReply;
import Messages.MessageShowAllEmployeesReply;
import Messages.MessageShowAllEmployees;
import Messages.MessageShowAllEmployeesReply;
import Messages.MessageShowAllWorkHours;
import Messages.MessageShowAllWorkHoursReply;
import Messages.MessageShowEmployeeIDReply;

/**
 * this class creates panel with functions for specific employee
 * 
 * @author igor,jeka
 *
 */
public class PanelFunctionsEmployeeThis extends PanelFunctionsEmpty {

	private static final long serialVersionUID = 1L;
	private MainMenu mainMenu;
	private String eid;
	private JButton button_editEmployee;
	private JButton button_DohotEmployee;
	private JButton button_delitEmployee;
	private JButton button_dohotEmployee;
	private JButton button_DeletEmployee;
	private JButton button_reportEmployee;
	private JButton button_ReportHours;
	
public PanelFunctionsEmployeeThis(MainMenu mm,String empId)
{
	super();
	this.mainMenu=mm;
	this.eid=empId;
	this.add(get_button_editEmployee());
	this.add(get_button_DeletEmployee());
	
	this.add(get_button_ReportEmployee());
	this.add(get_button_ReportHours()); 
	}
	
	
	
	/**
	 * creates button for opening edit specific employee screen
	 * 
	 * @return editing employee button
	 */
private JButton get_button_editEmployee() 
	{
		if (button_editEmployee == null) 
		{			
			button_editEmployee=new JButton("לעדכן  פרטי העובד");  			 
			button_editEmployee.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 15));
			button_editEmployee.setPreferredSize(new Dimension(220, 30));
			
			button_editEmployee.addActionListener(new java.awt.event.ActionListener() 
			   {
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.CENTER));
					MessageGetUserById getUser=new MessageGetUserById(eid);
					try {
						mainMenu.ms.client.sendToServer(getUser);	
					    MessageGetUserByIdReply result=(MessageGetUserByIdReply)mainMenu.ms.client.GetMessage();
					    mainMenu.getContentPane().add(new PfUpdate_employee(mainMenu,result.getEmployee()), BorderLayout.CENTER);
					    mainMenu.getContentPane().validate();
					    }	
					catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					
				}
			});
		}
		return button_editEmployee;
	}
	
	
private JButton get_button_ReportEmployee() 
{
	if (button_reportEmployee == null) 
	{			
		button_reportEmployee=new JButton("דוח מכירות");
		if(this.mainMenu.getUser().getPermission()==1)
			button_reportEmployee.setEnabled(false);
		button_reportEmployee.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 15));
		button_reportEmployee.setPreferredSize(new Dimension(220, 30));
		
		button_reportEmployee.addActionListener(new java.awt.event.ActionListener() 
		   {
			public void actionPerformed(java.awt.event.ActionEvent e) 
			{
				 Date dat = new Date();
				 DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
				 String   data = df.format(dat);
				 MessageGetReportWaitersByDate msgRep=new MessageGetReportWaitersByDate(data,data);
				try {
					mainMenu.ms.client.sendToServer(msgRep);
					MessageGetReportWaitersByDateReply result=(MessageGetReportWaitersByDateReply)mainMenu.ms.client.GetMessage();
					mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.CENTER));
					mainMenu.getContentPane().add(new PfReportOnSales(mainMenu,result.getReport()), BorderLayout.CENTER);					
					mainMenu.getContentPane().validate();
					
					
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
	}
	return button_reportEmployee;
}

	
private JButton get_button_DohotEmployee() 
	{
		if (button_DohotEmployee == null) 
		{			
			button_DohotEmployee=new JButton("דוחות");  			 
			button_DohotEmployee.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 15));
			button_DohotEmployee.setPreferredSize(new Dimension(220, 30));
			
			button_DohotEmployee.addActionListener(new java.awt.event.ActionListener() 
			   {
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					System.out.println("tables actionPerformed()"); 							
				}
			});
		}
		return button_DohotEmployee;
	}





/**
 * Delete specific employee
 * 
 * @return delete employee button
 */

private JButton get_button_DeletEmployee() 
{
	if (button_DeletEmployee == null) 
	{			
		button_DeletEmployee=new JButton("מחיקת עובד");  			 
		button_DeletEmployee.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 15));
		button_DeletEmployee.setPreferredSize(new Dimension(220, 30));
		
		button_DeletEmployee.addActionListener(new java.awt.event.ActionListener() 
		   {
			public void actionPerformed(java.awt.event.ActionEvent e) 
			{
				
				
				int n = JOptionPane.showConfirmDialog(
					    null,
					    "    אתה בטוח?",
					    "    בקשת אישור  ",
					    JOptionPane.YES_NO_OPTION);
				 //Object selectedValue = pane.getValue();
				 if(n==1){
					 
				 }
				 else{
					
					MessageDeleteEmployee msgDelEmp=new MessageDeleteEmployee (eid);
					try {
						mainMenu.ms.client.sendToServer(msgDelEmp);
						mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.CENTER));
						MessageShowAllEmployees msgShowSupp=new MessageShowAllEmployees( );
						mainMenu.ms.client.sendToServer(msgShowSupp);
						MessageShowAllEmployeesReply resultEmp=(MessageShowAllEmployeesReply)mainMenu.ms.client.GetMessage();
						mainMenu.getContentPane().add(new PfSearch_employee(mainMenu,resultEmp.getResult()), BorderLayout.CENTER);	
										
						mainMenu.getContentPane().validate();
					}
					catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
						
					}
			}}
		});
	}
	return button_DeletEmployee;
}
	
// --------  hours report of each employee

private JButton get_button_ReportHours() 
{
	if (button_ReportHours == null) 
	{			
		button_ReportHours=new JButton("דוח שעות");
	//	if(this.mainMenu.getUser().getPermission()==1)   //  getPermission()==1  chto znachit 1 ?
	//		button_ReportHours.setEnabled(false);
		button_ReportHours.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 15));
		button_ReportHours.setPreferredSize(new Dimension(220, 30));
		
		button_ReportHours.addActionListener(new java.awt.event.ActionListener() 
		   {
			public void actionPerformed(java.awt.event.ActionEvent e) 
			{
				 Date dat = new Date();
				 DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
				 String   data = df.format(dat);
				  
				 DateFormat dfhh = new SimpleDateFormat("MM");
				 String   months = dfhh.format(dat);
				 
				 MessageShowAllWorkHours msgShowHours=new MessageShowAllWorkHours(eid, months);    //send to server employeeID and number month for get work hours
				 
				 try {
					mainMenu.ms.client.sendToServer(msgShowHours);
				} catch (IOException e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				}
				
				try {
					MessageShowAllWorkHoursReply result2 = (MessageShowAllWorkHoursReply) mainMenu.ms.client.GetMessage();
					PfDoh_work_hoursAdmin empHours=new PfDoh_work_hoursAdmin(mainMenu,result2.getEmployeeHours());
					
				 
					mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.CENTER));
					
					mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.EAST));
				      
					mainMenu.getContentPane().add(new PanelFunctionsClockInAdmin(mainMenu,empHours), BorderLayout.EAST);
					mainMenu.getContentPane().add(empHours, BorderLayout.CENTER);	
					mainMenu.getContentPane().validate();
				
				} catch (IOException e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				}
 
			}
		});
	}
	return button_ReportHours;
}



}

	
	
	
	
	
	

